package com.kanshu.ksgb.fastread.doudou.module.signin.presenter;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.module.signin.bean.SignInBean;
import com.kanshu.ksgb.fastread.doudou.module.signin.bean.SignInResultBean;
import com.kanshu.ksgb.fastread.doudou.module.signin.event.SignInEvent;
import com.kanshu.ksgb.fastread.doudou.module.signin.retrofit.SingInService;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;

/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2017
 * Company: 成都古羌科技有限公司
 * CreateTime: 2017/12/19 18:38
 *
 * @author EvanHe
 * @version 1.0
 */
public class SignInPresenter extends BaseMvpPresenter {
    IGenrialMvpView<BaseResult<List<SignInBean>>> mSignInView;
    SingInService  singInService;

    public SignInPresenter(Subject lifeCyclerSubject) {
        super(lifeCyclerSubject);
        RetrofitHelper helper = RetrofitHelper.getInstance();
        singInService = helper.createService(SingInService.class);
    }

    public void setSignInView(IGenrialMvpView<BaseResult<List<SignInBean>>> mvpView) {
        mSignInView = mvpView;
    }

    public void getSignInInfos() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mSignInView != null)
                mSignInView.showError(Constants.ErrCode.NetErr,
                        Xutils.getContext().getString(R.string.standard_net_tip));
            return;
        }
        mSignInView.showLoading("");
        singInService.getSignInInfos("1")
                .compose(asyncRequest()).subscribe(new BaseObserver<List<SignInBean>>() {
            @Override
            public void onResponse(BaseResult<List<SignInBean>> listBaseResult, List<SignInBean> singInBeans, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                if (mSignInView != null && BaseResult.isNotNull(listBaseResult)) {
                    mSignInView.showContent(listBaseResult);
                    mSignInView.dismissLoading();
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                if (mSignInView != null) {
                    mSignInView.showError(code, errDesc);
                    mSignInView.dismissLoading();
                }
            }
        });
    }

    //签到
    public void signIn() {
        singInService.signIn()
                .compose(asyncRequest()).subscribe(new BaseObserver<SignInResultBean>() {

            @Override
            public void onResponse(BaseResult<SignInResultBean> result, SignInResultBean signInResultBean, Disposable disposable) {
                SignInEvent signInEvent = new SignInEvent();
                if (BaseResult.isNotNull(result)) {
                    int status = result.result.status.code;
                    switch (status) {
                        case SignInEvent.STATUS_SUCCESS:
                            signInEvent.status = SignInEvent.STATUS_SUCCESS;
                            signInEvent.coin = result.result.data.coin;
                            signInEvent.continueDay = result.result.data.continue_day;
                            break;
                        case SignInEvent.STATUS_ALREADY:
                            signInEvent.status = SignInEvent.STATUS_ALREADY;
                            break;
                        default:
                            signInEvent.status = SignInEvent.STATUS_FAILURE;
                            break;

                    }
                }
                EventBus.getDefault().post(signInEvent);
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                EventBus.getDefault().post(new SignInEvent(SignInEvent.STATUS_FAILURE));
            }
        });
    }


    public void isSignInToday(final INetCommCallback<SignInBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
       singInService.todayIsSignIn("1")
                .compose(asyncRequest()).subscribe(new BaseObserver<SignInBean>() {
            @Override
            public void onResponse(BaseResult<SignInBean> listBaseResult, SignInBean singInBeans, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onResponse(singInBeans);
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(code, errDesc);
            }
        });
    }

    @Override
    public void detachView() {
        super.detachView();
        mSignInView = null;
    }
}
